package com.linsir.ext.beetl;

import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.ext.web.SessionWrapper;

import com.linsir.common.utils.xx;
import com.linsir.config.EovaConst;
import com.linsir.model.MetaField;
import com.linsir.model.User;
import com.linsir.model.Widget;
/**
 * @author linsir
 * @title: Query
 * @projectName linsir-mu
 * @description: 模版数据查询函数
 * @date 2021/12/3 23:10
 */
public class Query implements Function{

    @Override
    public Object call(Object[] paras, Context ctx) {
        String type = paras[0].toString();
        if (type.equals("fields")) {
            // 元字段查询
            String objectCode = paras[1].toString();
            if (xx.isEmpty(objectCode) && !objectCode.matches("\\w+")) {
                throw new RuntimeException("参数异常，请输入合法参数");
            }
            // 获取当前会话用户
            SessionWrapper session = (SessionWrapper) ctx.getGlobal("session");
            User user = null;
            if (session != null) {
                user = (User) session.get(EovaConst.USER);
            }
            return MetaField.dao.queryFieldsGroup(objectCode, user);
        } else if (type.equals("widget")) {
            // 自定义插件查询
            return Widget.dao.findByType(Widget.TYPE_DIY);
        }
        return null;
    }
}
